home *** CD-ROM | disk | FTP | other *** search
- Path: newsroom.hitc.com!usenet
- From: psand@eos.hitc.com (G. Patrick Sand)
- Newsgroups: comp.lang.c++
- Subject: Re: How to handle error in constructor
- Date: 30 Jan 1996 17:12:54 GMT
- Organization: Hughes Aircraft (EOSDIS)
- Message-ID: <4eljin$pm@newsroom.hitc.com>
- References: <DLyyIM.5EG@teslab.lab.oz.au>
- NNTP-Posting-Host: 155.157.118.56
- Mime-Version: 1.0
- X-Newsreader: WinVN 0.99.3
-
- In article <DLyyIM.5EG@teslab.lab.oz.au>, andrew@teslab.lab.oz.au says...
- >
- >I'm converting and enhancing a simple program in C to C++. Without goin
- >g
- >into too much detail I have a class that represents a file on disk. The
- >contructor opens the file, but what should it do if the file cannot be
- >opened? The C program just calls fopen() tests the return value and
- >if there's an error it prints a message and exits.
- >
- >In C++ I thought to set a flag in the constructor and have a member func
- >tion
- >that tests the flag to see if the file was opened correctly. This seems
- >rather inelegant -- I guess exceptions would be the elegant way but seem
- >like overkill for such a simple program. I've looked in several C++ boo
- >ks but
- >error handling is given scant coverage except for exception handling.
- >
- >Any suggestions would be greatly appreciated.
- >
- >--
- >Andrew Phillips (News/Sys Admin) andrew@teslab.lab.oz.au +61 2 287 655
- >1
- >--------------------------------
- >Just a SPOKE, not a SPOKESPERSON
-
- Yes, I am dealing with the same issues in constructors on my project. I
- have been looking into the C++ FAQ (Q 43 amd 44; it was just recently
- posted in four parts) or in their book C++ FAQ (Cline and Lomow, ISBN
- 0-201-58958-3, about US$24). The book has some "skeletal" examples which
- illustrate various benefits/pitfalls of exception handling...
-
- I am vacillating between throwing an exception (discouraged here) and
- doing what you discussed: creating the object but having a status
- indicator to check before you use it--this approach is called either
- "half-baked" or "zombie" and allows you to clean things up gracefully.
- The exception approach was designed specifically for constructors and
- should--if you aren't tricky--automagically destruct things within
- reason. I'll post what I decide on and why, and look forward to other
- people's thoughts and examples on the matter...
-
- Hope this helps...
- --
- G. Patrick Sand
- psand@eos.hitc.com
- PatSand@aol.com
- (301) 925-0791
- "Travel Light But Right..."
- Microsoft Network is prohibited from redistributing
- this work in any form, in whole or in part. License
- to distribute this individual post is available to Microsoft
- for $999. Posting without permission constitutes an
- agreement to these terms...gps
-
-